/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-25     29922       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include "ssd1306.h"
#include <stdio.h>
#define DBG_TAG "oled"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

extern struct rt_messagequeue mq;

static void oled_entry(void* parameter)
{
    rt_uint8_t lightness;
    char str[3];
    ssd1306_Fill(Black);
    while(1)
    {
        if(rt_mq_recv(&mq,&lightness,sizeof(lightness),RT_WAITING_NO) > 0)
        {
            LOG_D("get lightness : %d", lightness);
            memset(str,0,sizeof(str));
            sprintf(str,"%02d",lightness);
            ssd1306_SetCursor(0, 15);
            ssd1306_WriteString("light value:", Font_11x18, White);
            ssd1306_SetCursor(55, 35);
            ssd1306_WriteString(str, Font_11x18, White);
            ssd1306_UpdateScreen();
        }
        rt_thread_mdelay(50);

    }
}

rt_err_t app_oled(void)
{

    ssd1306_Init();
    ssd1306_Fill(Black);
    ssd1306_SetCursor(30, 30);
    ssd1306_WriteString("loading", Font_11x18, White);
    ssd1306_UpdateScreen();

    rt_thread_t oled_t = rt_thread_create("oled_t", oled_entry, RT_NULL, 1024, 15, 5);
    if(oled_t == RT_NULL)
    {
        LOG_E("thread oled_t create failed\n");
        return -RT_ERROR;
    }
    if(RT_EOK != rt_thread_startup(oled_t))
    {
        LOG_E("thread oled_t startup failed\n");
        return -RT_ERROR;
    }
}
